home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.uni-koblenz.de!news
- From: Thomas Schweitzer <schweitz@informatik.uni-koblenz.de>
- Newsgroups: comp.lang.c++
- Subject: Problem: Virtual base class
- Date: 12 Jan 1996 09:52:11 GMT
- Organization: University of Koblenz, Germany
- Message-ID: <4d5b0b$m4b@newshost.uni-koblenz.de>
- NNTP-Posting-Host: grimm.uni-koblenz.de
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1N (X11; I; SunOS 4.1.3 sun4m)
- X-URL: news:comp.lang.c++/138657-139657
-
- Hi.
- I've got a problem concerning virtual base classes, multiple inheritance and
- casting. Consider the following class hierarchy:
-
- _____A_____
- | | |
- B virtual C
- | | |
- |_____|_____|
- D
-
- The problem: I can cast an instance of D to an instance of A. Having done this,
- the compiler forbids casting in the other direction. Now a little program:
-
- class A {
- int a;
- };
-
- class B : virtual public A {
- int b;
- };
-
- class C : virtual public A {
- int c;
- };
-
- class D : virtual public A, public B, public C {
- int d;
- };
-
- void main ()
- {
-
- A* a;
- D* d = new D,
- anotherD;
-
- a = (A*) d;
- anotherD = (D*) a; // Why? : cannot cast up from virtual baseclass `A'
-
- }
-
- Why is the casting in the last line not possible?
-
- Please reply via e-mail.
-
- Thanks in advance
- Thomas
-
-